"use strict";
const { dialog } = require('@electron/remote');
const simpleGit = require('simple-git');

class ProjectManager {
    constructor() {
        this.projects = [];
        this.projectListElement = document.getElementById('projectList');
        this.initializeEventListeners();
    }

    initializeEventListeners() {
        const addProjectButton = document.getElementById('addProject');
        const generateReportButton = document.getElementById('generateReport');

        if (addProjectButton) {
            addProjectButton.addEventListener('click', () => this.addProject());
        }

        if (generateReportButton) {
            generateReportButton.addEventListener('click', () => this.generateReport());
        }
    }

    async addProject() {
        const result = await dialog.showOpenDialog({
            properties: ['openDirectory']
        });

        if (!result.canceled && result.filePaths.length > 0) {
            const path = result.filePaths[0];
            const name = path.split('\\').pop() || path.split('/').pop() || path;
            
            this.projects.push({ path, name });
            this.renderProjects();
        }
    }

    renderProjects() {
        this.projectListElement.innerHTML = '';
        
        this.projects.forEach((project) => {
            const projectElement = document.createElement('div');
            projectElement.className = 'project-item';
            projectElement.textContent = `${project.name} (${project.path})`;
            this.projectListElement.appendChild(projectElement);
        });
    }

    async getGitChanges(projectPath) {
        const git = simpleGit(projectPath);
        
        try {
            const logs = await git.log({
                from: 'HEAD',
                since: '1 day'
            });

            const status = await git.status();

            return JSON.stringify({
                commits: logs.all,
                modified: status.modified,
                created: status.created,
                deleted: status.deleted
            }, null, 2);
        } catch (error) {
            console.error('获取Git变更失败:', error);
            return '';
        }
    }

    async generateReport() {
        for (const project of this.projects) {
            const changes = await this.getGitChanges(project.path);
            console.log(`项目 ${project.name} 的变更:`, changes);
        }
    }
}

// 初始化项目管理器
new ProjectManager(); 